/*
 * @Author: your name
 * @Date: 2020-12-30 09:36:44
 * @LastEditTime: 2021-01-23 23:10:08
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \node-server\app.js
 */
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var ejs = require('ejs');//html模板引擎解析
var app = express();
const myRouter = require('./routes')
// 解决跨域
//设置允许跨域访问该服务.
app.use(require('cors')())
// 配置Express 视图引擎 允许为html文件
app.engine('html', ejs.__express);
//视图引擎全局变量
app.set('view engine','html')
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

//挂载路由
app.get('/',(req, res, next)=>{
  res.send("欢迎e")
})
//路由挂载
myRouter(app)

//使用静态资源文件目录  不写前缀就是根路径
app.use('/public',express.static(path.join(__dirname, 'public'))); 
/* app.use('/admin/static',express.static(path.join(__dirname, '/views/admin/static')));
app.use('/web/static',express.static(path.join(__dirname, '/views/web/static'))); */
// catch 404 and forward to error handler
 app.use(function(req, res, next) {
  res.render(path.resolve(__dirname,'./views/404.html'))
  next(createError(404));
}); 
// error handler
 app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};
  // render the error page
  res.status(err.status || 500);
  res.render('error');
}); 

module.exports = app;
